
package {

    import flash.display.Sprite;
    import flash.display.LineScaleMode;
    import flash.display.CapsStyle;
    import flash.display.JointStyle;

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;

    public class ScoreIndicator extends Sprite {

	private var __width : int = 60;
	private var __height : int = 60;

	private var __value : int = 0;

	private var labelField : TextField;
	private var valueField : TextField;
	private var container : Sprite;
	private var labelFormat : TextFormat;
	private var valueFormat : TextFormat;
	private var labelFontSize : Number = 24;
	private var valueFontSize : Number = 32;

	public function ScoreIndicator(label:String) {

	    labelField = new TextField();
	    labelField.text = label;

	    labelFormat = new TextFormat();
	    labelFormat.size = labelFontSize;
	    labelFormat.color = 0x000000;

	    labelField.setTextFormat(labelFormat); 
	    labelField.autoSize = TextFieldAutoSize.CENTER;

	    container = new Sprite;

	    valueField = new TextField();
	    valueField.text = String(__value);

	    valueFormat = new TextFormat();
	    valueFormat.size = valueFontSize;
	    valueFormat.color = 0x000000;
	    
	    valueField.setTextFormat(valueFormat);
	    valueField.autoSize = TextFieldAutoSize.CENTER;

	    container.addChild(labelField);
	    container.addChild(valueField);
	    addChild(container);
	    redraw();
	}

	public function centerText() : void {
	    labelField.x = -labelField.textWidth/2;
	    valueField.x = -valueField.textWidth/2;
	    labelField.y = -__height/2;
	    valueField.y = -__height/2+labelField.textHeight;
	}

	public function redraw() : void {
	    var curLabelFontSize : Number = labelFontSize;
	    labelFormat.size = labelFontSize;
	    labelField.setTextFormat(labelFormat);
	    while((labelField.textWidth > __width) &&
		  (curLabelFontSize > 0)) {
		curLabelFontSize--;
		labelFormat.size = curLabelFontSize;
		labelField.setTextFormat(labelFormat);
	    }
	    var curValueFontSize : Number = valueFontSize;
	    valueFormat.size = valueFontSize;
	    valueField.setTextFormat(valueFormat);
	    while((__height - labelField.textHeight < valueField.textHeight)
		  && (curValueFontSize > 0)) {
		curValueFontSize--;
		valueFormat.size = curValueFontSize;
		valueField.setTextFormat(valueFormat);
	    }
	    while((valueField.textWidth > __width) && 
		  (valueFontSize > 0)) {
		trace("valueFontSize is: "+valueFontSize);
		valueFontSize--;
		valueFormat.size = valueFontSize;
		valueField.setTextFormat(valueFormat);
	    }

	    centerText();
	    centerContainer();
	}

	public function centerContainer() : void {
	    container.x = __width/2;
	    container.y = __height/2;
	}

	public function set labelTextColor(newColor:Object) : void {
	    labelFormat.color = newColor;
	    labelField.setTextFormat(labelFormat);
	}

	public function get labelTextColor() : Object {
	    return labelFormat.color;
	}

	public function set valueTextColor(newColor:Object) : void {
	    valueFormat.color = newColor;
	    valueField.setTextFormat(valueFormat);
	}

	public function get valueTextColor() : Object {
	    return valueFormat.color;
	}

	public function set labelTextSize(newSize:int) : void {
	    if (newSize > 0) {
		labelFontSize = newSize;
		labelFormat.size = newSize;
		labelField.setTextFormat(labelFormat); 
		redraw();
	    }
	}

	public function get labelTextSize() : int {
	    return labelFormat.size as int;
	}

	public function set valueTextSize(newSize:int) : void {
	    if (newSize > 0) {
		valueFontSize = newSize;
		valueFormat.size = newSize;
		valueField.setTextFormat(valueFormat); 
		redraw();
	    }
	}

	public function get valueTextSize() : int {
	    return valueFormat.size as int;
	}

	public function set displayWidth(newWidth:int):void {
	    if (newWidth > 0) {
		__width = newWidth;
		redraw();
	    }
	}

	public function get displayWidth():int {
	    return __width;
	}

	public function set displayHeight(newHeight:int):void {
	    if (newHeight > 0) {
		__height = newHeight;
		redraw();
	    }
	}

	public function get displayHeight():int {
	    return __height;
	}

	public function set value(newValue:int):void {
	    valueField.text = String(newValue);
	    __value = newValue;
	    valueField.setTextFormat(valueFormat);
	    redraw();
	}

	public function get value():int {
	    return __value;
	}

	public function set label(newLabel:String):void {
	    labelField.text = newLabel;
	    redraw();
	}

	public function get label():String {
	    return labelField.text;
	}
    }
}
